package com.zllkj.mm.api.network.subscriber;

import android.content.Intent;

import com.zllkj.mm.MyApp;
import com.zllkj.mm.api.network.exception.ApiErrorCode;
import com.zllkj.mm.api.network.exception.ApiException;
import com.zllkj.mm.common.L;
import com.zllkj.mm.module.UIHelper;
import com.zllkj.mm.util.common.NetworkUtils;

import java.io.IOException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

public abstract class BaseRxSubscriber<T> extends Subscriber<T> {

    @Override
    public void onError(Throwable e) {
        String errorMsg = "数据异常";

        if (e instanceof IOException) {
            if (NetworkUtils.isAvailable(MyApp.getInstance())) {
                errorMsg = "网络不可用" + e.getMessage();
            } else {
                errorMsg = "网络出错了" + e.getMessage();
            }
            onFail(errorMsg);
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
//            errorMsg = httpException.response().message();
            errorMsg = "网络连接异常";
            onFail(errorMsg);
        } else if (e instanceof ApiException) {
            ApiException exception = (ApiException) e;
            int code = exception.getErrorCode();
            onFail(code, e.getMessage());
        } else if (e instanceof ClassCastException) {
            errorMsg = "数据类型转换错误";
            onFail(errorMsg);
        } else {
            onFail(errorMsg);
        }

    }


    @Override
    public void onNext(T response) {
        onSucc(response);

    }

    @Override
    public void onCompleted() {

    }


    protected abstract void onSucc(T t);

    protected void onFail(String msg) {
        if (msg.contains("token")) {
            L.showToast("登录信息已失效");
            MyApp.getContext().sendBroadcast(new Intent("token_fail"));
            return;
        }
        L.showToast(msg);
    }

    protected void onFail(int code, String msg) {
        switch (code) {
            case ApiErrorCode.ERROR_SESSION_INVALID:
//                LocalBroadcastManager.getInstance(AppContext.getInstance()).sendBroadcast(new Intent(MDConstant.EXIT_APP));
                break;
            case ApiErrorCode.ERROR_BLACK:
                L.showToast("直播已结束！");
                break;
            case ApiErrorCode.ERROR_BLANCE_LACK:
                showBlanceLack();
                break;

        }

        onFail(msg);
    }

    private void showBlanceLack() {

//        final WeakReference<Activity> reference = new WeakReference<Activity>(AppManager.getAppManager().currentActivity());
//        final OutDialog outDialog = new OutDialog(reference.get(), "提示", "您当前余额不足,请立即充值！");
//        outDialog.setPositiveButton(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                outDialog.close();
//                UIHelper.showPayPage(reference.get());
//            }
//        });
//        outDialog.setNegativeButton(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                outDialog.close();
//            }
//        });
    }

}
